import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

export const devicesSlice = createSlice({
  name: 'devices',
  initialState: {
    RS422: [
      {
        id: 1,
        name: `RS422_1`,
        label: `RS422_1`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM7',
          baudRate: 115200
        }
      },
      {
        id: 2,
        name: `RS422_2`,
        label: `RS422_2`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM8',
          baudRate: 115200
        }
      },
      {
        id: 3,
        name: `RS422_3`,
        label: `RS422_3`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM9',
          baudRate: 115200
        }
      },
      {
        id: 4,
        name: `RS422_4`,
        label: `RS422_4`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM10',
          baudRate: 115200
        }
      },
      {
        id: 5,
        name: `RS422_5`,
        label: `RS422_5`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM11',
          baudRate: 115200
        }
      },
      {
        id: 6,
        name: `RS422_6`,
        label: `RS422_6`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM12',
          baudRate: 115200
        }
      },
      {
        id: 7,
        name: `RS422_7`,
        label: `RS422_7`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM13',
          baudRate: 115200
        }
      },
      {
        id: 8,
        name: `RS422_8`,
        label: `RS422_8`,
        defaultValue: 0,
        reaction: null,
        type: 'INOUT',
        connect: {
          path: 'COM14',
          baudRate: 115200
        }
      }
    ],
    computers: [
      {
        id: 1,
        name: '计算机1',
        cpuList: [
          {
            id: 1,
            name: 'RISCV',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.111'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          },
          {
            id: 2,
            name: 'MIPS',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.112'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          }
        ]
      },
      {
        id: 2,
        name: '计算机2',
        cpuList: [
          {
            id: 1,
            name: 'RISCV',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.121'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          },
          {
            id: 2,
            name: 'MIPS',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.122'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          }
        ]
      },
      {
        id: 3,
        name: '计算机3',
        cpuList: [
          {
            id: 1,
            name: 'RISCV',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.131'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          },
          {
            id: 2,
            name: 'MIPS',
            cpu: 0,
            memory: 0,
            fpga: null,
            nvram: null,
            sync: null,
            link: {
              port: 10010,
              host: '192.168.1.132'
            },
            interfaces: {
              GPIO: [
                // 10 个 TTL GPIO 输入   界面查询或者自动上报信息到界面
                ...Array.from({ length: 10 }, (_, i) => ({
                  id: i + 1,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输入
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 11,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 3 个 SYNC 输入（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 17,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'INPUT'
                })),
                // 6 个 15V GPIO 输出
                ...Array.from({ length: 6 }, (_, i) => ({
                  id: i + 20,
                  name: `GPIO_${i + 1}`,
                  label: `GPIO_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                })),
                // 3 个 SYNC 输出（机级）
                ...Array.from({ length: 3 }, (_, i) => ({
                  id: i + 26,
                  name: `SYNC_${i + 1}`,
                  label: `SYNC_${i + 1}`,
                  defaultValue: 0,
                  reaction: null,
                  type: 'OUTPUT'
                }))
              ]
            }
          }
        ]
      }
    ]
  },
  reducers: {
    updateStatus: (state, action) => {
      const { cpuNumber, computer: computerId, cpu, memory, fpga, nvram, sync } = action.payload

      state.computers = state.computers.map((item) => {
        if (item.id === computerId) {
          const cpuList = item.cpuList.map((comp) => {
            if (comp.id === cpuNumber) {
              return {
                ...comp,
                cpu,
                memory,
                fpga,
                nvram,
                sync
              }
            }
            return comp
          })
          return {
            ...item,
            cpuList
          }
        }
        return item
      })
    }
  }
})

const { updateStatus } = devicesSlice.actions
export { updateStatus }

export default devicesSlice.reducer
